package modules

import (
	"github.com/casbin/casbin/v2"
	gormadapter "github.com/casbin/gorm-adapter/v3"
	"gspeed/bootstrap/global"
)

type auth struct {
	enforcer *casbin.Enforcer
}

var Auth auth

func (a *auth) New() *casbin.Enforcer {
	if a.enforcer != nil {
		return a.enforcer
	}
	// mysql适配器
	adapter, err := gormadapter.NewAdapterByDB(global.DB)
	if err != nil {
		panic("适配器创建失败")
	}
	// 通过mysql适配器新建一个enforcer
	a.enforcer, err = casbin.NewEnforcer("./config/model.conf", adapter)
	// 日志记录
	a.enforcer.EnableLog(global.Config.GetBool("casbin.showLog"))
	return a.enforcer
}
